package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.current.CurrentStudentScoresInfo;
import com.grad.why.studentsystem.service.StudentScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentManageSystem")
public class StudentScoreController {
    @Autowired
    private StudentScoreService studentScoreService;

    @RequestMapping("/initTables")
    @ResponseBody
    public Object getPersonScores(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            if(search.equals("")){
                return null;
            }else{
                if(isDigits(search)) {
                    Integer id = Integer.parseInt(search);
                    List<CurrentStudentScoresInfo> studentScoresInfos = studentScoreService.getStudentScoresListById(id,offset,limit);
                    long total = studentScoreService.getStudentScoresListByIdLength(id);
                    Map<String, Object> result = new HashMap<>(2);
                    for(int i = 0;i<studentScoresInfos.size();i++){
                        studentScoresInfos.get(i).setStudentScoreId(i);
                    }
                    result.put("rows", studentScoresInfos);
                    result.put("total", total);
                    return result;
                }else{
                    String sname = search;
                    int count = studentScoreService.getStudentInfoByStudentName(sname);
                    if(count>1){
                        return null;
                    }else if(count == 0){
                        return null;
                    }else{
                        List<CurrentStudentScoresInfo> studentScoresInfos = studentScoreService.getStudentScoreByStudentName(sname,offset,limit);
                        long total = studentScoreService.getStudentScoreByStudentNameLength(search);
                        for(int i = 0;i<studentScoresInfos.size();i++){
                            studentScoresInfos.get(i).setStudentScoreId(i);
                        }
                        Map<String, Object> result = new HashMap<>(2);
                        result.put("rows", studentScoresInfos);
                        result.put("total", total);
                        return result;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public boolean isDigits(String ids){
        char[] ch = ids.toCharArray();
        for(char c : ch){
            if(!Character.isDigit(c)){
                return false;
            }
        }
        return true;
    }

    @RequestMapping("/getStudentInfoBySId/{sid}")
    @ResponseBody
    public Object getStudentInfoBySId(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order,
            @PathVariable("sid") Integer sid
    ) {
        try {
            List<CurrentStudentScoresInfo> studentScoresInfos = studentScoreService.getStudentScoresListById(sid,offset,limit);
            long total = studentScoreService.getStudentScoresListByIdLength(sid);
            Map<String, Object> result = new HashMap<>(2);
            for(int i = 0;i<studentScoresInfos.size();i++){
                studentScoresInfos.get(i).setStudentScoreId(i);
            }
            result.put("rows", studentScoresInfos);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
